package com.example.iteratorpattern.entity;

/**
 * @author ash
 * @description
 * @create 2023/2/20  23:50:59
 * @className ConcreteIteratorDesc
 * @verson 1.0.0
 */
public class ConcreteIteratorDesc extends Iterator{

    private ConcreteAggregate aggregate;
    private int current;

    public ConcreteIteratorDesc(ConcreteAggregate aggregate) {
        this.aggregate = aggregate;
        this.current =aggregate.count()-1;
    }

    @Override
    public Object first() {
        return aggregate.get(current);
    }

    @Override
    public Object next() {
        Object ret = null;
        current--;
        if (current >= 0){
            ret = aggregate.get(current);
        }
        return ret;
    }

    @Override
    public boolean isDone() {
        return current >= 0;
    }

    @Override
    public Object currentItem() {
        return aggregate.get(current);
    }
}
